<?php
/**
 * 基础数据服务类
 * @author 聂哥
 * @Date: 2022-12-29 18:44:21
*/
namespace app\common\service;

use app\admin\model\SystemAdmin;
use app\crm\model\BaseCourse;
use app\crm\model\CrmCustomer;
use think\Exception;

class BaseService
{
    protected static $allowList = ['campus','major','source','course','level','industry'];
    protected static $defaultNamespace = 'app\crm\model\\';

    /**
     * 获取校区列表
     * @MethodAuthor: 聂哥
     * @Date: 2023-01-11 14:46:00
     */
    public static function getCampusList()
    {
        return self::getDataList('campus');
    }

    /**
     * 获取专业列表
     * @MethodAuthor: 聂哥
     * @Date: 2023-01-11 14:46:00
     */
    public static function getMajorList()
    {
        return self::getDataList('major');
    }

    /**
     * 获取客户级别列表
     * @MethodAuthor: 聂哥
     * @Date: 2023-01-11 14:46:00
     */
    public static function getLevelList()
    {
        return self::getDataList('level');
    }

    /**
     * 获取客户行业列表
     * @MethodAuthor: 聂哥
     * @Date: 2023-01-11 14:46:00
     */
    public static function getIndustryList()
    {
        return self::getDataList('industry');
    }

    /**
     * 获取来源列表
     * @MethodAuthor: 聂哥
     * @Date: 2023-01-11 14:46:00
     */
    public static function getSourceList()
    {
        return self::getDataList('source');
    }

    /**
     * 获取客户所属列表
     * @MethodAuthor: 聂哥
     * @Date: 2023-01-11 14:46:00
     */
    public static function getCustomerOwnerList()
    {
//        $map['is_adviser'] = 1;
//        $map['status'] = 1;
//        $userList = SystemAdmin::where($map)
//                    ->all()
//                    ->column('username','id');
//        $newData = [];
//        foreach( $userList as $key=>$item ){
//            $newData[] = ['value'=>$key,'name'=>$item];
//        }
//        return $newData;
        $map['deal_status'] = 0;
        $uesrList = CrmCustomer::where($map)
            ->all()
            ->column('name','id');
        $newData = [];
        foreach( $uesrList as $key=>$item ){
            $newData[] = ['value'=>$key,'name'=>$item];
        }
        return $newData;
    }

    /**
     * 获取当前开班课程列表
     * @MethodAuthor: 聂哥
     * @Date: 2023-01-17 14:39:58
     */
    public static function getCurrentCourseList()
    {
        $courseList = BaseCourse::where('status',1)->select();
        $newData = [];
        
        foreach( $courseList as $key=>$item ){
            $tpl = sprintf('%s-￥%s',$item['name'],$item['price']);
            $newData[] = ['value'=>$item['id'],'name'=>$tpl];
        }

        return $newData;
    }

    /**
    * 获取课程列表
     * @MethodAuthor: 聂哥
     * @Date: 2022-12-29 18:44:21
     */
    public static function getCourseList()
    {
        $data = cache('cache_course_list');
        if ($data == null) {
            $data = BaseCourse::where('status', 1)->column('name', 'id');
            cache('cache_course_list', $data);
        }
        return $data;
    }

    /**
     * 获取跟进方式
     * @MethodAuthor: 聂哥
     * @Date: 2023-01-12 16:35:45
     */
    public static function getRecordMethodList()
    {
        return config('crm.record_method');
    }

    /**
     * 获取基础数据
     * @MethodAuthor: 聂哥
     * @Date: 2023-01-11 14:59:33
     */
    public static function getDataList($name,$isCache=true)
    {
        if( in_array($name,self::$allowList) == false ){
            return null;
        }
        $name = strtolower($name);
        $cacheKey = "base_{$name}_list";
        $data = cache($cacheKey);
        if( $data == null || $isCache == false ){
            $modelName = self::$defaultNamespace . 'Base' . ucfirst($name);
            $data = $modelName::all()->column('name','id');
            if( empty($data) ){
                throw new Exception("获取{$name}基础数据失败");
            }
            cache($cacheKey,$data);
        }
        return $data;
    }
}